package cc.rengu.igas.channel.upac.core.service.trans;


import cc.rengu.igas.channel.upac.common.constant.UpacTreeNodeConstant;
import cc.rengu.igas.channel.upac.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.upac.common.enums.UpacProdEnum;
import cc.rengu.igas.channel.upac.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.upac.core.service.base.TokenPayService;
import cc.rengu.igas.channel.upac.facade.request.TokenConsumeRequest;
import cc.rengu.igas.channel.upac.facade.response.TokenConsumeResponse;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.DstChannelInfo;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;

/**
 * 银联全渠道无跳转消费服务 on 2019/10/8.
 */
public class TokenConsumeService extends TokenPayService {

    @Override
    protected Object beforeBizProcess() throws Exception {
        TokenConsumeRequest tokenConsumeRequest = new TokenConsumeRequest();
        ConvertUtil.convertOutput(tokenConsumeRequest);
        return tokenConsumeRequest;
    }

    @Override
    protected ChnlBizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        int iReturnCode = 0;
        String acctEncFlag = xmlTreeUtil.getXmlTreeStringValue(UpacTreeNodeConstant.ACCT_ENC_FLAG);
        ChnlBizResponse chnlBizResponse = new ChnlBizResponse();
        TokenConsumeResponse tokenConsumeResponse = new TokenConsumeResponse();
        TokenConsumeRequest tokenConsumeRequest = (TokenConsumeRequest) request;
        DstChannelInfo dstChannelInfo = (DstChannelInfo) xmlTreeUtil.getXmlTreeObjectValue(UpacTreeNodeConstant.DST_CHANNEL_INFO, DstChannelInfo.class);
        ConvertUtil.convertInput(tokenConsumeRequest);
        /*token信息转换*/
        if (null != tokenConsumeRequest.getTokenPayInfo()) {
            packageTokenPayInfo(tokenConsumeRequest.getTokenPayInfo());
        }
        if (null != tokenConsumeRequest.getRiskCtrlInfo()) {
            packageRiskCtrlInfo(tokenConsumeRequest.getRiskCtrlInfo());
        }
        xmlTreeUtil.setXmlTreeStringValue(UpacTreeNodeConstant.txnType, "01");
        xmlTreeUtil.setXmlTreeStringValue(UpacTreeNodeConstant.txnSubType, "01");
        xmlTreeUtil.setXmlTreeStringValue(UpacTreeNodeConstant.bizType, UpacProdEnum.TOKEN_PAY.getProdCode());
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, tokenConsumeRequest.getHeader().getTxnNum());
        iReturnCode = schedule.callService(dstChannelInfo.getDstServiceName(), 0);
        if (98 == iReturnCode) {
            rglog.error("调用银联全渠道服务<{}>超时，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            tokenConsumeResponse.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
            tokenConsumeResponse.setRespDesc(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
            chnlBizResponse.setResult(tokenConsumeResponse);
            return chnlBizResponse;
        } else if (0 != iReturnCode) {
            rglog.error("调用银联全渠道服务<{}>失败，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }
        ConvertUtil.convertOutput(tokenConsumeResponse);
        tokenConsumeResponse.setRespCode(convertRespCode(tokenConsumeResponse.getRespCode()));

        if (RespCodeEnum.TRANS_SUCCESS.getRespCode().equals(tokenConsumeResponse.getRespCode())) {
            if (tokenConsumeResponse.getPayAcctNo() != null) {
                if ("Y".equals(acctEncFlag)) {
                    rglog.error("配置账户加密，解密账号");
                    tokenConsumeResponse.setPayAcctNo(descryptData(tokenConsumeResponse.getPayAcctNo()));
                }
            }
        }
        chnlBizResponse.setResult(tokenConsumeResponse);
        return chnlBizResponse;
    }

    @Override
    protected void afterBizProcess(ChnlBizResponse response) throws Exception {
        TokenConsumeResponse tokenConsumeResponse = (TokenConsumeResponse) response.getResult();
        ConvertUtil.convertInput(tokenConsumeResponse);
    }

}
